import time
import socket


class SocketUtils(object):
    def socket_client(self, server_info, data, timeout=5):
        if server_info["ip"] is None or server_info["port"] is None:
            return
        if data is None:
            return
        result = {"status": "fail"}
        # address = (server_info["ip"], server_info["port"])
        address = (server_info['ip'], server_info['port'])
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(address)
        s.settimeout(timeout)
        s.send(data.encode("utf-8"))
        try:
            result = {"status": "success", "result": s.recvfrom(1024)}
        except socket.timeout:
            self.__timeout_with_dosomething()
        s.close()
        print("连接结束")
        return result

    def __timeout_with_dosomething(self):
        print("连接超时")


if __name__ == "__main__":
    a = SocketUtils()
    s_info = {"ip": "192.168.1.199", "port": 2000}
    data = "52454313"
    print(a.socket_client(s_info, data))
